import { h } from 'vue'
import { shapes } from '../ViewManage/index'
import CustomLabel from './CustomLabel.vue'
import type { IndicatorsGraph } from '@/types'

function useRenderLabel() {
  const getLabel = (node: any) => {
    const d = node.data as IndicatorsGraph
    const { indicatorName, type } = d

    return `${indicatorName || '无'}${type ? `（${type}）` : ''}`
  }
  const hasParent = (node: any) => {
    const d = node.data as IndicatorsGraph
    // TODO
    return d.parentId > 0
  }
  const getValue = (node: any) => {
    const d = node.data as IndicatorsGraph
    const { yesterday, unit } = d

    const str = `${yesterday || ''} ${unit || ''}`
    return str.replace(/\s+/, '') ? `昨日用量：${str}` : str
  }
  const getValue1 = (node: any) => {
    const d = node.data as IndicatorsGraph
    const { today, unit } = d

    const str = `${today || ''} ${unit || ''}`
    return str.replace(/\s+/, '') ? `今日实时用量：${str}` : str
  }
  const getText = (node: any) => {
    const d = node.data as IndicatorsGraph
    const { pointName } = d

    return pointName || ''
  }

  function findShapeType(shape: string) {
    return shapes.find(v => v.value === shape)?.type
  }
  const getClass = (node: any) => {
    const d = node.data as IndicatorsGraph
    const { shape } = d
    let cls = ''

    const shapeType = shape ? findShapeType(shape) : ''
    if (shapeType) {
      cls += ` is-${shapeType}`
    }
    return cls
  }
  const treeNodeRender = (_e: any, node: any) => {
    return h(CustomLabel, {
      showLineArrow: hasParent(node),
      labelClass: getClass(node),
      labelText: getLabel(node),
      valueText: getValue(node),
      value1Text: getValue1(node),
      descText: getText(node),
    })
  }

  return {
    treeNodeRender,
  }
}

export default useRenderLabel
